package com.darrenchan.leetcode;

/**
 * @Desc
 * @Author chenchi03
 * @CreateTime 2020-01-24 09:17
 */
public class QLint553 {
    public int maxKilledEnemies(char[][] grid) {
        if (grid == null || grid.length == 0 || grid[0] == null || grid[0].length == 0) {
            return 0;
        }
        int m = grid.length;
        int n = grid[0].length;
        int[][] up = new int[m][n];
        int[][] down = new int[m][n];
        int[][] left = new int[m][n];
        int[][] right = new int[m][n];

        //上方向
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (i == 0) {
                    up[i][j] = grid[i][j] == 'E' ? 1 : 0;
                } else {
                    if (grid[i][j] == 'E') {
                        up[i][j] = up[i - 1][j] + 1;
                    } else if (grid[i][j] == '0') {
                        up[i][j] = up[i - 1][j];
                    } else {
                        up[i][j] = 0;
                    }
                }
            }
        }

        //下方向
        for (int i = m - 1; i >= 0; i--) {
            for (int j = 0; j < n; j++) {
                if (i == m - 1) {
                    down[i][j] = grid[i][j] == 'E' ? 1 : 0;
                } else {
                    if (grid[i][j] == 'E') {
                        down[i][j] = down[i + 1][j] + 1;
                    } else if (grid[i][j] == '0') {
                        down[i][j] = down[i + 1][j];
                    } else {
                        down[i][j] = 0;
                    }
                }
            }
        }


        //左方向
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (j == 0) {
                    left[i][j] = grid[i][j] == 'E' ? 1 : 0;
                } else {
                    if (grid[i][j] == 'E') {
                        left[i][j] = left[i][j - 1] + 1;
                    } else if (grid[i][j] == '0') {
                        left[i][j] = left[i][j - 1];
                    } else {
                        left[i][j] = 0;
                    }
                }
            }
        }


        //右方向
        for (int i = 0; i < m; i++) {
            for (int j = n - 1; j >= 0; j--) {
                if (j == n - 1) {
                    right[i][j] = grid[i][j] == 'E' ? 1 : 0;
                } else {
                    if (grid[i][j] == 'E') {
                        right[i][j] = right[i][j + 1] + 1;
                    } else if (grid[i][j] == '0') {
                        right[i][j] = right[i][j + 1];
                    } else {
                        right[i][j] = 0;
                    }
                }
            }
        }

        int max = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == '0') {
                    if (max < up[i][j] + down[i][j] + left[i][j] + right[i][j]) {
                        max = up[i][j] + down[i][j] + left[i][j] + right[i][j];
                    }
                }
            }
        }

        return max;
    }
}
